<?php
class ControllerSaleThongKeNhanh extends Controller {
	private $error = array();

	public function index() {
		$this->language->load('sale/customer');
		$this->load->model('sale/customer');
		$this->data['token'] = $this->session->data['token'];
		$this->document->setTitle('Thanh toán tiền thưởng');
		if(isset($this->request->get['customer_id'])){
			$this->data['action'] = $this->url->link('sale/thongkenhanh/activeTransaction', 'token=' . $this->session->data['token'] . '&customer_id=' . $this->request->get['customer_id'], 'SSL');
			$results = $this->model_sale_customer->getCustomer($this->request->get['customer_id']);
			if($results){
				$this->data['transaction'] = $this->config->get('config_price_level'.$results['level1']);
				$data = array(
					'customer_parent' => $this->request->get['customer_id']
				);
				$this->data['name'] = $results['name'];
				$this->data['madn'] = $results['madn'];
				$this->data['date_added'] = date($this->language->get('date_format_short'), strtotime($results['date_added']));
				$this->data['color_level'] = $this->config->get('config_color_level'.$results['level1']);
				if((int)$results['level1']){
					$this->data['level'] = 'Đại lý';
				}elseif((int)$results['level1'] == 1 || (int)$results['level1'] == 2 || (int)$results['level1'] == 3 || (int)$results['level1'] == 4){
					$this->data['level'] = 'Quản Lý ' . (int)$results['level1'];
				} elseif ((int)$results['level1'] == 5) {
					$this->data['level'] = 'Quản lý Pro';
				} else {
					$this->data['level'] = ' Cổ đông Cty';
				}
				$customers = $this->model_sale_customer->getCustomersForTransactionLevel($data);
				$this->data['customers'] = array();
				if($customers){
					foreach ($customers as $result) {
						$customer_parent_info = $this->model_sale_customer->getCustomerForHH($result['customer_parent']);
						$color_level = $this->config->get('config_color_level'.$result['level2']);
						if((int)$result['level2'] == 0 || (int)$result['level2'] == 1 || (int)$result['level2'] == 2 || (int)$result['level2'] == 3 || (int)$result['level2'] == 4){
							$level = 'Quản Lý ' . (int)$result['level2'];
						} elseif ((int)$result['level2'] == 5) {
							$level = 'Quản lý Pro';
						} else {
							$level = ' Cổ đông Cty';
						}
						$this->data['customers'][] = array(
							'customer_id'    => $result['customer_id'],
							'madn'           => $result['madn'],
							'name'           => $result['name'],
							'color_level'    => $color_level,
							'email'          => $result['email'],
							'level'          => $level,
							'status_id'      => $result['status'],
							'date_added'     => date($this->language->get('date_format_short'), strtotime($result['date_added']))
						);
					}
				}
			}
		}	
		$this->template = 'sale/thong_ke_nhanh.tpl';	
		$this->children = array(
			'common/footer',
			'common/header'	
		);
		$this->response->setOutput($this->render());	
	}
	public function activeTransaction() {
		$this->language->load('sale/customer');
		$this->load->model('sale/customer');
		$results = $this->model_sale_customer->getCustomer($this->request->get['customer_id']);
		if($results){
			$this->model_sale_customer->addTransactionsCustomer($this->request->get['customer_id'], $this->request->get['total'], $this->request->get['comment']);
			$this->updateLevelForCustomerTransaction($this->request->get['customer_id']);
			$this->session->data['success'] = 'Hệ thống thanh toán thành công cho thành viên ' . $results['madn'];
		} else {
			$this->session->data['warning'] = 'Vui long kiểm tra lại, thành viên không tồn tại trong hệ thống';
		}
		$this->redirect($this->url->link('sale/customer', 'token=' . $this->session->data['token'], 'SSL'));
	}
	
	//Des: Update level for customer in process transaction HH by level
	public function updateLevelForCustomerTransaction($customer_id){
		//Input: customer_id
		
	}
}
?>